package com.hxh.demo.controller;

import com.hxh.demo.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/7/6 1:01
 */
@Controller // Spring 启动时加载
@ResponseBody // 返回非页面数据
@RequestMapping("/user") // 路由规则注册
public class UserController {

    // 路由映射演示
    @RequestMapping("/hi")
    public String sayHello() {
        return "Hello, 黄小黄~";
    }

    // 指定 POST
    @RequestMapping(value = "/showPost", method = RequestMethod.POST)
    public String showPost() {
        return "接收到了 POST 请求~";
    }

    // 指定 GET
    @GetMapping("/showGet")
    public String showGet() {
        return "接收到了 GET 请求~";
    }

    // 传递单个参数
    @GetMapping("/method01")
    public String method01(int num) {
        return "num: " + num;
    }

    // 传递单个参数
    @GetMapping("/method02")
    public String method02(Integer num) {
        return "num: " + num;
    }

    // 传递对象
    @GetMapping("/student")
    public String showStudent(Student student) {
        String name = student.getName();
        String password = student.getPassword();
        return name + " : " + password;
    }

    // 参数映射
    @GetMapping("/para")
    public String showCurName(@RequestParam(value = "name", required = false) String curName) {
        return "curName = " + curName;
    }

    // 传递 JSON
    @PostMapping("/set-student")
    public String setStudent(@RequestBody Student student) {
        System.out.println(student);
        return "Student Set Success.";
    }

    // 获取 URL 中的参数
    @GetMapping("/login/{name}/{password}")
    public String login(@PathVariable String name, @PathVariable String password) {
        return "name = " + name + " | " +
                "password = " + password + " | " +
                "Login Success.";
    }

    // 上传文件Demo
    @RequestMapping("/upFile")
    public String upFile(@RequestParam("myFile") MultipartFile file) throws IOException {
        // 上传的路径根目录
        String path = "D:\\info\\";
        // 路径+【唯一文件名】
        path += UUID.randomUUID().toString().replace("-", "");
        // 路径+文件名+【后缀】
        path += file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        // 保存文件
        file.transferTo(new File(path));
        return path + "上传成功！";
    }

    // 传统获取 header/cookie
    @RequestMapping("/method04")
    public String method04(HttpServletRequest req, HttpServletResponse resp) {
        String name = req.getParameter("name");
        // 获取所有 Cookie 信息
        Cookie[] cookies = req.getCookies();
        // 获取 header 信息
        String userAgent = req.getHeader("User-Agent");
        return name + " : " + userAgent;
    }

    // @CookieValue 获取 Cookie
    @RequestMapping("/cookie")
    public String cookie(@CookieValue("name") String name) {
        return "cookie: " + name;
    }

    // @RequestHeader 获取 header
    @RequestMapping("/header")
    public String header(@RequestHeader("User-Agent") String userAgent) {
        return "User-Agent: " + userAgent;
    }

    //  Session 可以使⽤ HttpServletRequest
    @RequestMapping("/sess1")
    public String sess1(HttpServletRequest req) {
        // 如果 session 不存在, 不会自动创建
        HttpSession session = req.getSession(false);
        String username = "默认值";
        if (session != null && session.getAttribute("username") != null) {
            username = (String) session.getAttribute("username");
        }
        return username;
    }

    // 更简洁方式获取 Session
    @RequestMapping("/sess2")
    public String sess2(@SessionAttribute(value = "username", required = false) String username) {
        return username;
    }
}
